package files;

import java.awt.Color;
import java.util.ArrayList;

import display.Theme;



public class TetrisData {

	private int level;
	private String player;
	private int score;

	private ArrayList<Theme> themes;
	private ArrayList<ScoreObject> scores;
	private ArrayList<String> players;
	
	private int lastThemeUsedIndex;

	public TetrisData() {
		player = "Invité";
		score = 0;
		level = 0;
		lastThemeUsedIndex = 0;

		themes = new ArrayList<Theme>();
		scores = new ArrayList<ScoreObject>();
		players = new ArrayList<String>();
		
		Theme themeBasique = new Theme();
		themeBasique.setColorBlockA(new Color(4,111,193)); //Color.RED);
		themeBasique.setColorBlockB(new Color(0,179,87));//Color.CYAN);
		themeBasique.setColorBlockC(new Color(1,51,150));//Color.GREEN);
		themeBasique.setColorBlockD(new Color(235,39,41));//Color.BLUE);
		themeBasique.setColorBlockE(new Color(246,133,52));//Color.PINK);
		themeBasique.setColorBlockF(new Color(113,49,161));//127, 255, 0));
		themeBasique.setColorBlockG(new Color(255,125,125));//Color.MAGENTA);
		themeBasique.setName("Basique");
		themes.add(themeBasique);
		players.add(player);


	}
	
	public void saveAll(){
		saveColors();
		saveScore();
		savePlayers();
	}

	public void loadAll(){
		loadColors();
		loadScores();
		loadPlayers();
	}

	public void saveColors(){
		boolean exists = false;
		int i = 0;

		while(i < themes.size() && !exists){
			exists  = themes.get(i).equals(application.Utils.getCurrentTheme());
			i++;
		}

		lastThemeUsedIndex = i-1;

		if(!exists){
			lastThemeUsedIndex = themes.size();
			application.Utils.getCurrentTheme().setName("Personalisé " + lastThemeUsedIndex);
			themes.add(application.Utils.getCurrentTheme());
			//System.out.println("Enregistrement du thème : Personalisé " + lastThemeUsedIndex);
		}
	}
	
	public void saveScore(){
		// Sauvegarde les nbScoresMax meilleurs scores
		int nbScoresMax = 10;
		boolean added = false;
		
		if(score != 0){
			if(scores.size() == 0){
				scores.add(new ScoreObject(player, score));
			}else{
				for(int i = 0; i < scores.size() && !added; i++){
					if(scores.get(i).getScore() < score){
						scores.add(i,new ScoreObject(player, score));
						added = true;
					}
				}
				
				
				if(!added)
					scores.add(scores.size(),new ScoreObject(player, score));
				
				if(scores.size() > nbScoresMax){
					scores.remove(nbScoresMax);
				}
			}
			
			//System.out.println("Enregistrement du score : " + score + " pour le joueur " + player);
		}
	}
	
	public void savePlayers(){
		boolean exists = false;
		int i = 0;

		while(i < players.size() && !exists){
			exists  = players.get(i).equals(player);
			i++;
		}

		if(!exists){
			players.add(player);
			//System.out.println("Enregistrement du joueur : " + player);
		}
		
	//	System.out.println(players.size());
	}


	
	public void loadColors(){
		application.Utils.setThemes(themes);

		application.Utils.setCurrentTheme(lastThemeUsedIndex);

		//System.out.println("Mise en place du thème : " + application.Utils.getThemes().get(lastThemeUsedIndex).getName());
	}
	
	public void loadScores(){
		application.Utils.setScores(scores);
		score = 0;
	}
	
	public void loadPlayers(){
		/*int size = players.size();
		String[] StringPlayers = new String[size];
		application.Utils.cmbPlayers = StringPlayers;
		
		System.out.println("Dernier joueur ayant joué : " + player);*/
	}
	


	public String getPlayer() {
		return player;
	}


	public void setPlayer(String player) {
		//System.out.println("Joueur changé de " + this.player + " vers " + player);
		this.player = player;
	}

	public void addPlayer(String player){
		this.setPlayer(player);
		this.savePlayers();
		this.loadPlayers();
	}

	public int getScore() {
		return score;
	}
	
	public String getScoreHTML(){
		String returned = "<html>Meilleurs scores :<br><br>";
		
		for(ScoreObject o : scores){
			returned += "<br>" + o.getPlayer() + " : " + o.getScore();
		}
		
		returned += "</html>";
		
		return returned;
	}


	public void setScore(int score) {
		this.score = score;
	}


	public int getLevel() {
		return level;
	}


	public void setLevel(int level) {
		this.level = level;
	}

	public ArrayList<Theme> getThemes() {
		return themes;
	}

	public void setThemes(ArrayList<Theme> themes) {
		this.themes = themes;
	}

	public int getLastThemeUsedIndex() {
		return lastThemeUsedIndex;
	}

	public void setLastThemeUsedIndex(int lastThemeUsedIndex) {
		this.lastThemeUsedIndex = lastThemeUsedIndex;
	}

	public ArrayList<ScoreObject> getScores() {
		return scores;
	}

	public void setScores(ArrayList<ScoreObject> scores) {
		this.scores = scores;
	}


	public ArrayList<String> getPlayers() {
		return players;
	}


	public void setPlayers(ArrayList<String> players) {
		this.players = players;
	}

	

}



